package com.sskj.b70.p.activity;

import com.lzy.okgo.model.Response;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.rxbus2.RxBus;
import com.sskj.b70.m.HttpConfig;
import com.sskj.b70.m.bean.HttpData;
import com.sskj.b70.m.bean.PayWayBean;
import com.sskj.b70.m.bean.SysConfigBean;
import com.sskj.b70.m.bean.rxbus.TabChange;
import com.sskj.b70.m.http.JsonCallBack;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.activity.FabiPublishActivity;

import java.util.List;

import io.reactivex.Flowable;

/**
 * ProjectName：bfex
 * DESC: (类描述)
 * Created by 李岩 on 2018/7/3 0003
 * updateName:(修改人名称)
 * updateTime:(修改时间)
 * updateDesc:(修改内容)
 */
public class FabiPublishPresenter extends BasePresenter<FabiPublishActivity> {
    public void requsetPayWayList() {
        httpService.getPayList()
                .execute(new JsonCallBack<HttpData<List<PayWayBean>>>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData<List<PayWayBean>>> response) {
                        mView.onPayWaySuccess(response.body().getData());
                    }

                    @Override
                    public void onError(Response<HttpData<List<PayWayBean>>> response) {
                        super.onError(response);
                        mView.onHttpFail();
                    }
                });
    }

    /**
     * @param type       pmma求购/sell出售
     * @param sumCount
     * @param lowCount
     * @param hightCount
     * @param unitPrice
     * @param remark
     */
    public void requsetPublish(String type, String sumCount, String lowCount,
                               String hightCount, String unitPrice,
                               int payWx, int payAli, int payBank, String remark) {
        httpService.createFabiOrder(type, sumCount, lowCount, hightCount, unitPrice, payWx, payAli, payBank, remark)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == HttpConfig.STATUS_OK) {
                            RxBus.getDefault().send(Constants.FABI_DATING_REFRESH);
                            RxBus.getDefault().post(new TabChange("pmma".equals(type)));
                            mView.finish();
                        }
                    }
                });

    }

    public void requestUnitPrice() {
        httpService.getSysConfig(3)
                .execute(new JsonCallBack<HttpData<List<SysConfigBean>>>() {
                    @Override
                    protected void _onSuccess(Response<HttpData<List<SysConfigBean>>> response) {
                        Flowable.fromIterable(response.body().getData())
                                .filter(item -> "ck_price".equals(item.getName()))
                                .subscribe(item -> mView.onPublishPriceSuccess(item.getValue()));
                    }
                });

    }
}
